/*
 * Written by Seung Il Moon (simoon@networking.khu.ac.kr)
 * 2011.01.04
 * 
 */

package net.m2m.policyeditor.model;

import java.sql.*;

public class ConnectPolicyRepository {
	private Connection conn;
	private String mysqlDriver;
	private String mysqlUrl;
	private String mysqlCharEncoding;
	private String userID;
	private String userPW;

	public ConnectPolicyRepository() {
		conn = null;
		mysqlDriver = "org.gjt.mm.mysql.Driver";
		mysqlUrl = "jdbc:mysql://localhost/mydb";
		mysqlCharEncoding = "?characterEncoding=utf-8";
		userID = "root";
		userPW = "networking2987";
	}
	
	public Connection Connection() {
		try {
			Class.forName(mysqlDriver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		try {
			StringBuffer url = new StringBuffer(mysqlUrl);
			url.append(mysqlCharEncoding);
			conn = DriverManager.getConnection(url.toString(), userID, userPW);			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return conn;
	}
	
	public void setMysqlDriver(String mysqlDriver) {
		this.mysqlDriver = mysqlDriver;
	}

	public void setMysqlUrl(String mysqlUrl) {
		this.mysqlUrl = mysqlUrl;
	}

	public void setMysqlCharEncoding(String mysqlCharEncoding) {
		this.mysqlCharEncoding = mysqlCharEncoding;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public void setUserPW(String userPW) {
		this.userPW = userPW;
	}
	
	public void closeConn() {
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
